/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterDlg.java
 *
 * Created on Nov 16, 2009, 4:48:34 PM
 */
package cititool.chat.client.UI;

import cititool.chat.client.ClientContext;
import cititool.chat.model.SystemConstants;
import cititool.chat.protocol.TransProtocol;
import cititool.model.UserInfo;
import cititool.util.ComponentHelper;
import cititool.util.EncryptHelper;
import cititool.util.ImageHelper;
import cititool.util.StringHelper;
import cititool.util.SwingThreadPool;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.JTextComponent;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author zx04741
 */
public class RegisterDlg extends javax.swing.JDialog {

    /** Creates new form RegisterDlg */
    public RegisterDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initdata();
    }

    public void setHost(String host, int port) {

        this.host = host;
        this.port = port;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        genderRadio = new javax.swing.ButtonGroup();
        registerPane = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        r_username = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        r_pass = new javax.swing.JPasswordField();
        jLabel9 = new javax.swing.JLabel();
        r_pass2 = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        nickname = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        countrySel = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        maleRadio = new javax.swing.JRadioButton();
        femaleRadio = new javax.swing.JRadioButton();
        jLabel17 = new javax.swing.JLabel();
        address = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        r_cancelBtn = new javax.swing.JButton();
        photoview = new javax.swing.JLabel();
        browser = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        photopath = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        birth = new javax.swing.JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
        jLabel20 = new javax.swing.JLabel();
        submitBtn = new javax.swing.JButton();
        phoneNo = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cititool.MainApp.class).getContext().getResourceMap(RegisterDlg.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        registerPane.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("registerPane.border.title"))); // NOI18N
        registerPane.setName("registerPane"); // NOI18N
        registerPane.setPreferredSize(new java.awt.Dimension(723, 431));

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        r_username.setName("r_username"); // NOI18N

        jLabel6.setForeground(resourceMap.getColor("jLabel6.foreground")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        r_pass.setName("r_pass"); // NOI18N
        r_pass.setNextFocusableComponent(r_pass2);

        jLabel9.setForeground(resourceMap.getColor("jLabel9.foreground")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        r_pass2.setName("r_pass2"); // NOI18N

        jLabel10.setForeground(resourceMap.getColor("jLabel10.foreground")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        nickname.setName("nickname"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        countrySel.setName("countrySel"); // NOI18N

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jLabel15.setForeground(resourceMap.getColor("jLabel15.foreground")); // NOI18N
        jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        genderRadio.add(maleRadio);
        maleRadio.setSelected(true);
        maleRadio.setText(resourceMap.getString("maleRadio.text")); // NOI18N
        maleRadio.setName("maleRadio"); // NOI18N

        genderRadio.add(femaleRadio);
        femaleRadio.setText(resourceMap.getString("femaleRadio.text")); // NOI18N
        femaleRadio.setName("femaleRadio"); // NOI18N

        jLabel17.setText(resourceMap.getString("jLabel17.text")); // NOI18N
        jLabel17.setName("jLabel17"); // NOI18N

        address.setName("address"); // NOI18N

        jLabel18.setText(resourceMap.getString("jLabel18.text")); // NOI18N
        jLabel18.setName("jLabel18"); // NOI18N

        r_cancelBtn.setText(resourceMap.getString("r_cancelBtn.text")); // NOI18N
        r_cancelBtn.setName("r_cancelBtn"); // NOI18N
        r_cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                r_cancelBtnActionPerformed(evt);
            }
        });

        photoview.setText(resourceMap.getString("photoview.text")); // NOI18N
        photoview.setBorder(new javax.swing.border.LineBorder(resourceMap.getColor("photoview.border.lineColor"), 2, true)); // NOI18N
        photoview.setName("photoview"); // NOI18N

        browser.setText(resourceMap.getString("browser.text")); // NOI18N
        browser.setName("browser"); // NOI18N
        browser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browserActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        photopath.setEditable(false);
        photopath.setText(resourceMap.getString("photopath.text")); // NOI18N
        photopath.setName("photopath"); // NOI18N

        jLabel19.setForeground(resourceMap.getColor("jLabel19.foreground")); // NOI18N
        jLabel19.setText(resourceMap.getString("jLabel19.text")); // NOI18N
        jLabel19.setName("jLabel19"); // NOI18N

        birth.setText(resourceMap.getString("birth.text")); // NOI18N
        birth.setToolTipText(resourceMap.getString("birth.toolTipText")); // NOI18N
        birth.setName("birth"); // NOI18N

        jLabel20.setForeground(resourceMap.getColor("jLabel20.foreground")); // NOI18N
        jLabel20.setText(resourceMap.getString("jLabel20.text")); // NOI18N
        jLabel20.setName("jLabel20"); // NOI18N

        submitBtn.setText(resourceMap.getString("submitBtn.text")); // NOI18N
        submitBtn.setName("submitBtn"); // NOI18N
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });

        phoneNo.setText(resourceMap.getString("phoneNo.text")); // NOI18N
        phoneNo.setName("phoneNo"); // NOI18N

        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout registerPaneLayout = new javax.swing.GroupLayout(registerPane);
        registerPane.setLayout(registerPaneLayout);
        registerPaneLayout.setHorizontalGroup(
            registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(registerPaneLayout.createSequentialGroup()
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(registerPaneLayout.createSequentialGroup()
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel13))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(registerPaneLayout.createSequentialGroup()
                                        .addComponent(maleRadio)
                                        .addGap(18, 18, 18)
                                        .addComponent(femaleRadio))
                                    .addGroup(registerPaneLayout.createSequentialGroup()
                                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(r_pass2)
                                                .addComponent(r_pass)
                                                .addComponent(r_username, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                                            .addGroup(registerPaneLayout.createSequentialGroup()
                                                .addComponent(birth, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel20)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel10)
                                            .addComponent(jLabel9)
                                            .addComponent(jLabel18)
                                            .addComponent(jLabel11)
                                            .addComponent(jLabel6))))
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(registerPaneLayout.createSequentialGroup()
                                        .addGap(4, 4, 4)
                                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(registerPaneLayout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(browser)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jLabel1))
                                            .addGroup(registerPaneLayout.createSequentialGroup()
                                                .addGap(2, 2, 2)
                                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(phoneNo, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                                                    .addComponent(nickname, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel15)
                                                    .addComponent(jLabel19)))))
                                    .addGroup(registerPaneLayout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(photopath, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(photoview, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(166, 166, 166))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, registerPaneLayout.createSequentialGroup()
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel17)
                                    .addComponent(jLabel12))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(countrySel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(address, javax.swing.GroupLayout.PREFERRED_SIZE, 573, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(157, 157, 157)))
                        .addGap(67, 67, 67))
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addGap(217, 217, 217)
                        .addComponent(submitBtn)
                        .addGap(72, 72, 72)
                        .addComponent(r_cancelBtn)))
                .addContainerGap())
        );
        registerPaneLayout.setVerticalGroup(
            registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(registerPaneLayout.createSequentialGroup()
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(r_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(r_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(18, 18, 18)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(r_pass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10))))
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(browser)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(photopath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(maleRadio)
                                .addComponent(femaleRadio)
                                .addComponent(jLabel18)
                                .addComponent(jLabel19)
                                .addComponent(phoneNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel16)))
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(photoview, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, registerPaneLayout.createSequentialGroup()
                                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel11)
                                    .addComponent(nickname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel15))
                                .addGap(26, 26, 26))
                            .addGroup(registerPaneLayout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(registerPaneLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(birth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13)
                            .addComponent(jLabel20))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(countrySel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(59, 59, 59)
                .addGroup(registerPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitBtn)
                    .addComponent(r_cancelBtn))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(registerPane, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(registerPane, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void r_cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_r_cancelBtnActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_r_cancelBtnActionPerformed

    private void browserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browserActionPerformed

        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (chooser.showDialog(this, null) != JFileChooser.APPROVE_OPTION) {
            return;
        }
        final File f = chooser.getSelectedFile();
        if(!ImageHelper.isImageFile(f.getName())){
            JOptionPane.showMessageDialog(this, "the file is not format image!");
            return;
        }
         
        photopath.setText(f.getPath());
        SwingThreadPool.execute(new Runnable() {
            public void run() {
                try {
                    Image image = ImageIO.read(f);
                    Dimension d = photoview.getBounds().getSize();
                    BufferedImage buffer = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
                    buffer.getGraphics().drawImage(image, 0, 0, d.width, d.height, null);
                    photoview.setIcon(new ImageIcon(buffer));
                    photoview.repaint();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(rootPane,"file read error:"+ex.getMessage());
                }
            }
        });
}//GEN-LAST:event_browserActionPerformed

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        // TODO add your handling code here:
       try {
            // TODO add your handling code here:
           JTextComponent[] coms={r_username,r_pass,r_pass2,birth,nickname};
           if(ComponentHelper.checkEmpty(coms, this))
               return ;
           if(!String.valueOf(r_pass.getPassword()).equals(String.valueOf(r_pass2.getPassword()))){
               JOptionPane.showMessageDialog(this, "two password don't match!");
               return;
           }

            UserInfo userinfo = new UserInfo();
            userinfo.setId(StringHelper.UUID());
            userinfo.setNickname(nickname.getText());
            userinfo.setUsername(r_username.getText());            
            userinfo.setPass(EncryptHelper.encodeMd5(String.valueOf(r_pass.getPassword()))  );
            int gender = 0;
            if (maleRadio.isSelected()) {
                gender = 1;
            }
            if (femaleRadio.isSelected()) {
                gender = 2;
            }
            userinfo.setGender(gender + "");
            userinfo.setPhoneNo(phoneNo.getText());
            userinfo.setBirthday(birth.getText());
            userinfo.setAddress(address.getText());
            userinfo.setCountry(countrySel.getSelectedItem().toString());
            userinfo.setPhotopath(photopath.getText());

            Socket s = new Socket(host, port);
            ClientContext.registerUser(userinfo, s,null);
            String rstr = TransProtocol.getObject(s).toString();
            if (rstr.equals(SystemConstants.REG_SUC + "")) {
                JOptionPane.showMessageDialog(this, "register success!");
                this.dispose();
                s.close();

            } else if(rstr.equals(SystemConstants.REG_FAIL + "")) {
                JOptionPane.showMessageDialog(this, "register failure!");
            }else if(rstr.equals(SystemConstants.REG_REP + "")){
                JOptionPane.showMessageDialog(this, "the username exists!");
            }

        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "register failure!host connection erro!please check host!");
            ComponentHelper.jtaAppendLine(log, ex.toString());
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(this,  "register failure!host connection erro!please check host!");
            ComponentHelper.jtaAppendLine(log, ex.toString());
        }
    }//GEN-LAST:event_submitBtnActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                RegisterDlg dialog = new RegisterDlg(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField address;
    private javax.swing.JFormattedTextField birth;
    private javax.swing.JButton browser;
    private javax.swing.JComboBox countrySel;
    private javax.swing.JRadioButton femaleRadio;
    private javax.swing.ButtonGroup genderRadio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton maleRadio;
    private javax.swing.JTextField nickname;
    private javax.swing.JFormattedTextField phoneNo;
    private javax.swing.JTextField photopath;
    private javax.swing.JLabel photoview;
    private javax.swing.JButton r_cancelBtn;
    private javax.swing.JPasswordField r_pass;
    private javax.swing.JPasswordField r_pass2;
    private javax.swing.JTextField r_username;
    private javax.swing.JPanel registerPane;
    private javax.swing.JButton submitBtn;
    // End of variables declaration//GEN-END:variables
    private String host;
    private int port;
    private JTextArea log;

    private void initdata() {
        try {
            log = ((LoginFrame) this.getParent()).getLog();
            countrySel.addItem("CN");
            countrySel.addItem("EN");
            countrySel.addItem("US");
            countrySel.addItem("SG");
            MaskFormatter mask = new MaskFormatter("###########");
            phoneNo.setFormatterFactory(new DefaultFormatterFactory(mask));
        } catch (ParseException ex) {
            Logger.getLogger(RegisterDlg.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
